package cn.zysuyuan.user.service.impl;

import cn.zysuyuan.common.entity.Constant;
import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.utils.BeanMapUtils;
import cn.zysuyuan.common.utils.UUIDUtil;
import cn.zysuyuan.user.client.RoleClient;
import cn.zysuyuan.user.entity.User;
import cn.zysuyuan.user.entity.UserUpdate;
import cn.zysuyuan.user.mapper.UserMapper;
import cn.zysuyuan.user.service.UserService;
import cn.zysuyuan.user.utils.CodecUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019-11-25 16:36
 */

@Service
public class UserServiceImpl implements UserService {

    private Integer page;

    private Integer size;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {

        // 1、生成随机id
        user.setId(UUIDUtil.uuid());
        // 2、生成盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);

        if (user.getuType() == 1) {
            user.setLevel("admin");
        } else {
            user.setLevel("user");
        }

        if (StringUtils.isBlank(user.getuPass())) {
            user.setuPass(Constant.USER_INITIAL_PASSWORD);
        }

        if (StringUtils.isBlank(user.getuHeadpath())) {
            user.setuHeadpath(Constant.USER_HEAD_PATH);
        }
        // 3、加盐加密
        user.setuPass(CodecUtils.md5Hex(user.getuPass(),salt));

        user.setuCreateTime(new Date());

        this.userMapper.insertUser(user);
    }

    @Override
    public User selectUser(String uName, String uPass) {
        User record = new User();
        record.setuName(uName);
        User user = this.userMapper.selectUser(record);

        if (user == null) {
            return null;
        }

        // 获取盐，对用户输入的密码加盐加密
        uPass = CodecUtils.md5Hex(uPass,user.getSalt());

        // 和数据库中的密码进行比较
        if (StringUtils.equals(uPass,user.getuPass())) {
            System.out.println(user);
            return user;
        }
        return null;
    }

    @Override
    public void updateUser(UserUpdate user) {
        if (StringUtils.isNotBlank(user.getuPass())) {
            // 1、生成盐
            String salt = CodecUtils.generateSalt();
            user.setSalt(salt);
            System.out.println("密码" + user.getuPass());
            System.out.println("盐：" + salt);
            // 2、加盐加密
            user.setuPass(CodecUtils.md5Hex(user.getuPass(),salt));
        }
        if (user.getuType() != null) {
            if (user.getuType() == 1) {
                user.setLevel("admin");
            } else {
                user.setLevel("user");
            }
        }

        System.out.println(user.getSalt());
        this.userMapper.updateUser(user);
    }

    @Override
    public int queryUserName(String username) {
        return userMapper.queryUserName(username);
    }

    @Override
    public int queryNickName(String nickname) {
        return userMapper.queryNickName(nickname);
    }

    @Override
    public PageResult<User> selectAllUser(Map<String, Object> map) {
        if (map.get("page") != null && map.get("page") != ""){
            this.page = Integer.parseInt((String) map.get("page"));
        }else{
            this.page = 1;
        }
        if (map.get("size") != null &&  map.get("size") != ""){
            this.size = Integer.parseInt((String) map.get("size"));
        }else{
            this.size = 5;
        }
        PageHelper.startPage(this.page,this.size);
//        this.page = Integer.parseInt((String) map.get("page"));
//        this.size = Integer.parseInt((String) map.get("size"));
//        PageHelper.startPage(this.page,this.size);
        List<User> list = this.userMapper.selectAllUser(map);
//        System.out.println(list.get(0));
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return new PageResult<>(list);
    }

    @Override
    public int resetPassword(String id) {

        UserUpdate user = new UserUpdate();

        user.setId(id);
        // 1、生成盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);
        System.out.println("密码" + user.getuPass());
        System.out.println("盐：" + salt);
        // 2、加盐加密
        user.setuPass(CodecUtils.md5Hex(Constant.USER_INITIAL_PASSWORD,salt));

        return userMapper.resetPassword(user);
    }

    @Override
    public int deleteUser(String id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public User getRoleList(String id) {
        User user = userMapper.findById(id);
        return  user;
    }
}
